package com.cskaoyan.controller.wxorder;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.CommonData;
import com.cskaoyan.bean.MemoryBean;
import com.cskaoyan.bean.bo.wxorder.OrderBo;
import com.cskaoyan.bean.bo.wxorder.WxCommenBo;
import com.cskaoyan.bean.bo.wxorder.WxOrderSubmit;
import com.cskaoyan.bean.po.ohter.MarketOrderGoods;
import com.cskaoyan.bean.po.wxorder.MarketComment;
import com.cskaoyan.bean.vo.wxorder.OrderListVo;
import com.cskaoyan.bean.vo.wxorder.WxOrderDetail;
import com.cskaoyan.bean.vo.wxorder.WxOrderSubmitVo;
import com.cskaoyan.exception.MarketValidationException;
import com.cskaoyan.service.wx_cart.WxCartService;
import com.cskaoyan.service.wxorder.WxOrderService;
import com.cskaoyan.util.beanutils.BeanUtil;
import com.cskaoyan.util.common.BeanTransfer;
import com.cskaoyan.util.common.Constant;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Zhang Tao
 * @version 1.0.0
 * @description
 * @date since 2022-07-19 10:32
 */
@RestController
@RequestMapping("wx/order")
public class WxOrderController {
    @Autowired
    WxOrderService wxOrderService;
    @Autowired
    MemoryBean memoryBean;

    @GetMapping("list")
    public BaseRespVo orderList(@RequestHeader(Constant.WX_TOKEN_HEADER) String token,
                                OrderBo orderBo) throws MarketValidationException {

        Integer id = memoryBean.tokenAndUserId.get(token);
        if(id==null){
            throw new MarketValidationException("请重新登陆！");
        }
        CommonData<OrderListVo> baseResp =
                wxOrderService.queryOrderListByUserId(orderBo, id);
        return BaseRespVo.ok(baseResp);

    }

    @PostMapping("comment")
    public BaseRespVo orderComment(@RequestBody WxCommenBo comment,@RequestHeader(Constant.WX_TOKEN_HEADER) String token) throws IllegalAccessException, MarketValidationException {

        Integer id = memoryBean.tokenAndUserId.get(token);
        if(id==null){
            throw new MarketValidationException("请重新登陆！");
        }
        MarketComment marketComment = new MarketComment();
        MarketComment marketComment1 = (MarketComment) BeanTransfer.transfer(comment, marketComment);
        marketComment1.setUserId(id);
        marketComment1.setValueId(comment.getOrderGoodsId());
        wxOrderService.insertComment(marketComment1);
        return BaseRespVo.ok();
    }

    @GetMapping("detail")
    public BaseRespVo orderDetail(Integer orderId){
        WxOrderDetail detail=wxOrderService.queryOrderDetail(orderId);
        return BaseRespVo.ok(detail);
    }
    @PostMapping("refund")
    public BaseRespVo orderRefund(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");

        wxOrderService.orderRefund(orderId);
       return BaseRespVo.ok();

    }
    @PostMapping("delete")
    public BaseRespVo orderDelete(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");
        wxOrderService.orderDelete(orderId);
        return BaseRespVo.ok();

    }
    @PostMapping("confirm")
    public BaseRespVo orderConfirm(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");

        wxOrderService.orderConfirm(orderId);
        return BaseRespVo.ok();
    }
    @GetMapping("cancel")
    public BaseRespVo orderCancel(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");

        wxOrderService.orderCancel(orderId);
        return BaseRespVo.ok();

    }
    @GetMapping("goods")
    public BaseRespVo orderGoods(Integer orderId,Integer goodsId){

        MarketOrderGoods orderGoods = wxOrderService.queryOrderGoods(orderId,goodsId);
        return BaseRespVo.ok(orderGoods);
    }

    // @PostMapping("prepay")
    // public BaseRespVo orderPrepay(@RequestBody Integer orderId){
    //    return BaseRespVo.ok();
    // }
    // @PostMapping("submit")
    // public BaseRespVo orderSubmit(@RequestBody )
    @RequestMapping("prepay")
    public BaseRespVo prepay(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        wxOrderService.prepay(orderId);
        return BaseRespVo.ok();
    }


    @RequestMapping("submit")
    public BaseRespVo submit(@RequestHeader(Constant.WX_TOKEN_HEADER) String token,
                             @RequestBody WxOrderSubmit submitBO) throws MarketValidationException {
        Integer userId = memoryBean.tokenAndUserId.get(token);
        if(userId==null){
            throw new MarketValidationException("请重新登陆！");
        }
        if (submitBO.getCartId() == 0) {
            //zhong:cartId为0时，下单全部
            //找到所有cartId
            List<Integer> cartIds = wxOrderService.getCartIds(userId);
            ArrayList<WxOrderSubmitVo> wxOrderSubmitVos = new ArrayList<>();
            for (Integer cartId : cartIds) {
                WxOrderSubmit wxOrderSubmit = new WxOrderSubmit();
                BeanUtil.copyBean(submitBO,wxOrderSubmit);
                wxOrderSubmit.setCartId(cartId);
                WxOrderSubmitVo data=wxOrderService.submit(wxOrderSubmit,userId);
                wxOrderSubmitVos.add(data);
            }
            return BaseRespVo.ok(wxOrderSubmitVos.get(0));
        }

        WxOrderSubmitVo data=wxOrderService.submit(submitBO,userId);
        return BaseRespVo.ok(data);
    }
}
